package com.fairytask.extention;

import java.sql.SQLException;

import com.fairytask.extention.define.CustomToClientDefine;
import com.fairytask.extention.user.UserManager;
import com.qq.open.OpensnsException;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

public class UserInfoHandler extends BaseClientRequestHandler {
	private LoginExtention loginExt;
	private User pUser;
	@Override
	public void handleClientRequest(User user, ISFSObject params) {
		pUser	=	user;
		loginExt	=	(LoginExtention)getParentExtension();
		//send()
		
		UserManager	userMgr	=	new UserManager(loginExt);
		
		System.out.println("==============================");
		System.out.println("openid"+params.getUtfString("openid").toString());
		System.out.println("openkey"+params.getUtfString("openkey").toString());
		System.out.println("==============================");
		userMgr.initApi(params.getUtfString("openid"), params.getUtfString("openkey"),user);
		
		String userInfo	=	"{}";
		SFSObject	userObj		=	SFSObject.newFromJsonData(userInfo);
		try
		{
			userObj	=	userMgr.getUserInfo();
		} 
		catch (OpensnsException e)
		{
			e.printStackTrace();
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		if(userObj!=null)
		{
			//doUserDb(userObj);
			loginExt.send(CustomToClientDefine.USER_INFO, userObj, user);
		}
		else
		{
			System.out.println("========user not login");
		}
	}

	/**
	 * 检查用户属性 并记录数据
	 * @param userObj
	 */
	private void doUserDb(SFSObject userObj)
	{
		UserManager	userMgr	=	new UserManager(loginExt);
		userMgr.userCheck(userObj,pUser);
	}

}
